<?php

namespace Util;


class Comm extends \Lib\Util\Comm {

    public static function encrypt($string) {
        $key = 'jyg123';
        return parent::crypt($string, 'E' , $key);
    }

    public static function decrypt($string) {
        $key = 'jyg123';
        return parent::crypt($string, 'D' , $key);
    }

    public static function showImage($file) {
        $path = dirname(APP_DIR) . '/Uploads';
        $file = $path . '/' . $file;
        if (!file_exists($file)) {
            header('HTTP/1.1 404 Not Found');
            header("status: 404 Not Found");
            \Main::sysExit();
        }
        $image = new \Imagick($file);
        header('Content-Type: image/jpeg');
        $image->setImageFormat('jpeg');
        echo $image;
    }

    public static function strSplitCn($str) {
        $result = array();
        $len = mb_strlen($str);
        for ($i = 0; $i < $len; $i++) {
            $result[] = mb_substr($str, $i, 1);
        }
        return $result;
    }

    public static function encryptId($id, $key = 'jyg123') {
        $data = $id;
        $data = str_pad($data, 8, chr(0));
        $encrypted = mcrypt_encrypt(MCRYPT_3DES, $key, $data, MCRYPT_MODE_ECB);
        $h = 'H16';
        if (strlen($data) > 8) {
            $h = 'H32';
        }
        list(, $hex) = unpack($h, $encrypted);
        $return = 'U' . $hex;
        return $return;
    }

    public static function decryptId($id, $key = 'jyg123') {
        $hex = substr($id, 1);
        $len = strlen($hex);
        if ($len != 16 && $len != 32) {
            return null;
        }
        $encrypted = pack('H' . $len, $hex);
        $decrypted = mcrypt_decrypt(MCRYPT_3DES, $key, $encrypted, MCRYPT_MODE_ECB);
        return rtrim($decrypted);
    }

}